<!DOCTYPE HTML>
<html lang="en" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>part1_3_cache_config - learning-gem5</title>
        <!-- Custom HTML head -->
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="../favicon.svg">
        <link rel="shortcut icon" href="../favicon.png">
        <link rel="stylesheet" href="../css/variables.css">
        <link rel="stylesheet" href="../css/general.css">
        <link rel="stylesheet" href="../css/chrome.css">
        <link rel="stylesheet" href="../css/print.css" media="print">
        <!-- Fonts -->
        <link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../fonts/fonts.css">
        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../highlight.css">
        <link rel="stylesheet" href="../tomorrow-night.css">
        <link rel="stylesheet" href="../ayu-highlight.css">

        <!-- Custom theme stylesheets -->
    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><a href="../part0_introduction.html">Learning gem-5</a></li><li class="chapter-item expanded "><a href="../part0_introduction.html"><strong aria-hidden="true">1.</strong> part0_introduction</a></li><li class="chapter-item expanded "><a href="../part1/part1_1_building.html"><strong aria-hidden="true">2.</strong> part1</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part1/part1_1_building.html"><strong aria-hidden="true">2.1.</strong> part1_1_building</a></li><li class="chapter-item expanded "><a href="../part1/part1_2_simple_config.html"><strong aria-hidden="true">2.2.</strong> part1_2_simple_config</a></li><li class="chapter-item expanded "><a href="../part1/part1_3_cache_config.html" class="active"><strong aria-hidden="true">2.3.</strong> part1_3_cache_config</a></li><li class="chapter-item expanded "><a href="../part1/part1_4_gem5_stats.html"><strong aria-hidden="true">2.4.</strong> part1_4_gem5_stats</a></li><li class="chapter-item expanded "><a href="../part1/part1_5_gem5_example_configs.html"><strong aria-hidden="true">2.5.</strong> part1_5_gem5_example_configs</a></li><li class="chapter-item expanded "><a href="../part1/part1_6_extending_configs.html"><strong aria-hidden="true">2.6.</strong> part1_6_extending_configs</a></li></ol></li><li class="chapter-item expanded "><a href="../part2/part2_0_environment.html"><strong aria-hidden="true">3.</strong> part2</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part2/part2_0_environment.html"><strong aria-hidden="true">3.1.</strong> part2_0_environment</a></li><li class="chapter-item expanded "><a href="../part2/part2_1_helloobject.html"><strong aria-hidden="true">3.2.</strong> part2_1_helloobject</a></li><li class="chapter-item expanded "><a href="../part2/part2_2_debugging.html"><strong aria-hidden="true">3.3.</strong> part2_2_debugging</a></li><li class="chapter-item expanded "><a href="../part2/part2_3_events.html"><strong aria-hidden="true">3.4.</strong> part2_3_events</a></li><li class="chapter-item expanded "><a href="../part2/part2_4_parameters.html"><strong aria-hidden="true">3.5.</strong> part2_4_parameters</a></li><li class="chapter-item expanded "><a href="../part2/part2_5_memoryobject.html"><strong aria-hidden="true">3.6.</strong> part2_5_memoryobject</a></li><li class="chapter-item expanded "><a href="../part2/part2_6_simplecache.html"><strong aria-hidden="true">3.7.</strong> part2_6_simplecache</a></li><li class="chapter-item expanded "><a href="../part2/part2_7_arm_power_modelling.html"><strong aria-hidden="true">3.8.</strong> part2_7_arm_power_modelling</a></li><li class="chapter-item expanded "><a href="../part2/part2_8_arm_dvfs_support.html"><strong aria-hidden="true">3.9.</strong> part2_8_arm_dvfs_support</a></li></ol></li><li class="chapter-item expanded "><a href="../part3/part3_00_MSIntro.html"><strong aria-hidden="true">4.</strong> part3</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../part3/part3_00_MSIntro.html"><strong aria-hidden="true">4.1.</strong> part3_00_MSIntro</a></li><li class="chapter-item expanded "><a href="../part3/part3_01_cache-intro.html"><strong aria-hidden="true">4.2.</strong> part3_01_cache-intro</a></li><li class="chapter-item expanded "><a href="../part3/part3_02_cache-declarations.html"><strong aria-hidden="true">4.3.</strong> part3_02_cache-declarations</a></li><li class="chapter-item expanded "><a href="../part3/part3_03_cache-in-ports.html"><strong aria-hidden="true">4.4.</strong> part3_03_cache-in-ports</a></li><li class="chapter-item expanded "><a href="../part3/part3_04_cache_actions.html"><strong aria-hidden="true">4.5.</strong> part3_04_cache_actions</a></li><li class="chapter-item expanded "><a href="../part3/part3_05_cache_transitions.html"><strong aria-hidden="true">4.6.</strong> part3_05_cache_transitions</a></li><li class="chapter-item expanded "><a href="../part3/part3_06_directory.html"><strong aria-hidden="true">4.7.</strong> part3_06_directory</a></li><li class="chapter-item expanded "><a href="../part3/part3_07_MSIbuilding.html"><strong aria-hidden="true">4.8.</strong> part3_07_MSIbuilding</a></li><li class="chapter-item expanded "><a href="../part3/part3_08_configuration.html"><strong aria-hidden="true">4.9.</strong> part3_08_configuration</a></li><li class="chapter-item expanded "><a href="../part3/part3_09_running.html"><strong aria-hidden="true">4.10.</strong> part3_09_running</a></li><li class="chapter-item expanded "><a href="../part3/part3_10_MSIdebugging.html"><strong aria-hidden="true">4.11.</strong> part3_10_MSIdebugging</a></li><li class="chapter-item expanded "><a href="../part3/part3_11_simple-MI_example.html"><strong aria-hidden="true">4.12.</strong> part3_11_simple-MI_example</a></li></ol></li><li class="chapter-item expanded "><a href="../part4_gem5_101.html"><strong aria-hidden="true">5.</strong> part4_gem5_101</a></li><li class="chapter-item expanded "><a href="../http://doxygen.gem5.org/develop/index.html"><strong aria-hidden="true">6.</strong> part4_gem5_102</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">learning-gem5</h1>

                    <div class="right-buttons">
                        <a href="../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>
                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <hr />
<h2>layout: documentation
title: Adding cache to configuration script
doc: Learning gem5
parent: part1
permalink: /documentation/learning_gem5/part1/cache_config/
author: Jason Lowe-Power</h2>
<h1 id="将缓存添加到配置脚本"><a class="header" href="#将缓存添加到配置脚本">将缓存添加到配置脚本</a></h1>
<p>以<a href="http://www.gem5.org/documentation/learning_gem5/part1/simple_config/">前面的配置脚本为起点</a>，本章将逐步完成一个更复杂的配置。我们将向系统添加缓存层次结构，如下图所示。此外，本章将介绍理解 gem5 统计输出和向脚本添加命令行参数。</p>
<p><img src="part1_3_cache_config.assets/advanced_config.png" alt="具有两级缓存层次结构的系统配置。" /></p>
<h2 id="创建缓存对象"><a class="header" href="#创建缓存对象">创建缓存对象</a></h2>
<p>我们将使用经典缓存，而不是 ruby-intro-chapter，因为我们正在对单个 CPU 系统进行建模并且我们不关心建模缓存一致性。我们将扩展 Cache SimObject 并为我们的系统配置它。首先，我们必须了解用于配置 Cache 对象的参数。</p>
<blockquote>
<p><strong>经典缓存和 Ruby</strong></p>
<p>gem5 目前有两个完全不同的子系统来模拟系统中的片上缓存，“经典缓存”和“Ruby”。其历史原因是 gem5 是来自密歇根州的 m5 和来自威斯康星州的 GEMS 的组合。GEMS 使用 Ruby 作为其缓存模型，而经典缓存来自 m5 代码库（因此称为“经典”）。这两种模型之间的区别在于，Ruby 旨在对缓存一致性进行详细建模。Ruby 的一部分是 SLICC，一种用于定义缓存一致性协议的语言。另一方面，经典缓存实现了简化且不灵活的 MOESI 一致性协议。</p>
<p>要选择要使用的模型，您应该问问自己要模拟什么。如果您正在对缓存一致性协议的更改进行建模，或者一致性协议可能对您的结果产生一级影响，请使用 Ruby。否则，如果一致性协议对您不重要，请使用经典缓存。</p>
<p>gem5 的一个长期目标是将这两种缓存模型统一为一个整体模型。</p>
</blockquote>
<h3 id="缓存"><a class="header" href="#缓存">缓存</a></h3>
<p>Cache SimObject 声明可以在 src/mem/cache/Cache.py 中找到。这个 Python 文件定义了您可以设置 SimObject 的参数。在幕后，当 SimObject 被实例化时，这些参数被传递给对象的 C++ 实现。在 <code>Cache</code>从SimObject继承<code>BaseCache</code>对象如下所示。</p>
<p>在<code>BaseCache</code>类中，有许多<em>参数</em>。例如，<code>assoc</code>是一个整数参数。一些参数，比如 <code>write_buffers</code>有一个默认值，在这种情况下是 8。默认参数是 的第一个参数<code>Param.*</code>，除非第一个参数是字符串。每个参数的字符串参数是对参数是什么的描述（例如， <code>tag_latency = Param.Cycles(&quot;Tag lookup latency&quot;)</code>意味着 <code>tag_latency</code>控制“该缓存的命中延迟”）。</p>
<p>其中许多参数没有默认值，因此我们需要在调用之前设置这些参数<code>m5.instantiate()</code>。</p>
<hr />
<p>现在，要创建具有特定参数的缓存，我们首先要<code>caches.py</code>在与 simple.py 相同的目录中 创建一个新文件<code>configs/tutorial</code>。第一步是导入我们要在这个文件中扩展的 SimObject(s)。</p>
<pre><code>from m5.objects import Cache
</code></pre>
<p>接下来，我们可以像对待任何其他 Python 类一样对待 BaseCache 对象并对其进行扩展。我们可以随意命名新缓存。让我们从制作 L1 缓存开始。</p>
<pre><code>class L1Cache(Cache):
    assoc = 2
    tag_latency = 2
    data_latency = 2
    response_latency = 2
    mshrs = 4
    tgts_per_mshr = 20
</code></pre>
<p>在这里，我们正在设置 BaseCache 的一些没有默认值的参数。要查看所有可能的配置选项，并找出哪些是必需的，哪些是可选的，您必须查看 SimObject 的源代码。在这种情况下，我们使用 BaseCache。</p>
<p>我们已经扩展<code>BaseCache</code>并设置了<code>BaseCache</code>SimObject 中没有默认值的大部分参数。接下来，让我们再来两个 L1Cache 的子类，一个 L1DCache 和 L1ICache</p>
<pre><code>class L1ICache(L1Cache):
    size = '16kB'

class L1DCache(L1Cache):
    size = '64kB'
</code></pre>
<p>让我们也创建一个带有一些合理参数的 L2 缓存。</p>
<pre><code>class L2Cache(Cache):
    size = '256kB'
    assoc = 8
    tag_latency = 20
    data_latency = 20
    response_latency = 20
    mshrs = 20
    tgts_per_mshr = 12
</code></pre>
<p>现在我们已经指定了 所需的所有必要参数 <code>BaseCache</code>，我们所要做的就是实例化我们的子类并将缓存连接到互连。但是，将大量对象连接到复杂的互连可能会使配置文件快速增长并变得不可读。因此，让我们首先为我们的子类添加一些辅助函数<code>Cache</code>。记住，这些只是 Python 类，所以我们可以用它们做任何你可以用 Python 类做的事情。</p>
<p>让我们为 L1 缓存添加两个功能，<code>connectCPU</code>将 CPU 连接到缓存和<code>connectBus</code>将缓存连接到总线。我们需要将以下代码添加到<code>L1Cache</code>类中。</p>
<pre><code>def connectCPU(self, cpu):
    # need to define this in a base class!
    raise NotImplementedError

def connectBus(self, bus):
    self.mem_side = bus.cpu_side_ports
</code></pre>
<p>接下来，我们必须<code>connectCPU</code>为指令和数据缓存定义一个单独的函数，因为 I-cache 和 D-cache 端口具有不同的名称。我们的<code>L1ICache</code>和<code>L1DCache</code>类现在变成：</p>
<pre><code>class L1ICache(L1Cache):
    size = '16kB'

    def connectCPU(self, cpu):
        self.cpu_side = cpu.icache_port

class L1DCache(L1Cache):
    size = '64kB'

    def connectCPU(self, cpu):
        self.cpu_side = cpu.dcache_port
</code></pre>
<p>最后，让我们添加函数以<code>L2Cache</code>分别连接到内存端和 CPU 端总线。</p>
<pre><code>def connectCPUSideBus(self, bus):
    self.cpu_side = bus.mem_side_ports

def connectMemSideBus(self, bus):
    self.mem_side = bus.cpu_side_ports
</code></pre>
<p>完整的文件可以在 gem5 源文件中找到 <a href="https://gem5.googlesource.com/public/gem5/+/refs/heads/stable/configs/learning_gem5/part1/caches.py"><code>configs/learning_gem5/part1/caches.py</code></a>。</p>
<h2 id="将缓存添加到简单的配置文件"><a class="header" href="#将缓存添加到简单的配置文件">将缓存添加到简单的配置文件</a></h2>
<p>现在，让我们将刚刚创建的缓存添加到我们在<a href="http://www.gem5.org/documentation/learning_gem5/part1/simple_config/">上一章中</a>创建的配置脚本中。</p>
<p>首先，让我们将脚本复制到一个新名称。</p>
<pre><code>cp ./configs/tutorial/simple.py ./configs/tutorial/two_level.py
</code></pre>
<p>首先，我们需要将<code>caches.py</code>文件中的名称导入命名空间。我们可以将以下内容添加到文件顶部（在 m5.objects 导入之后），就像使用任何 Python 源一样。</p>
<pre><code>from caches import *
</code></pre>
<p>现在，在创建 CPU 之后，让我们创建 L1 缓存：</p>
<pre><code>system.cpu.icache = L1ICache()
system.cpu.dcache = L1DCache()
</code></pre>
<p>并使用我们创建的辅助函数将缓存连接到 CPU 端口。</p>
<pre><code>system.cpu.icache.connectCPU(system.cpu)
system.cpu.dcache.connectCPU(system.cpu)
</code></pre>
<p>您需要<em>删除</em>将缓存端口直接连接到内存总线的以下两行。</p>
<pre><code>system.cpu.icache_port = system.membus.cpu_side_ports
system.cpu.dcache_port = system.membus.cpu_side_ports
</code></pre>
<p>我们不能直接将 L1 缓存连接到 L2 缓存，因为 L2 缓存只需要一个端口连接到它。因此，我们需要创建一个 L2 总线来将我们的 L1 缓存连接到 L2 缓存。我们可以使用我们的辅助函数将 L1 缓存连接到 L2 总线。</p>
<pre><code>system.l2bus = L2XBar()

system.cpu.icache.connectBus(system.l2bus)
system.cpu.dcache.connectBus(system.l2bus)
</code></pre>
<p>接下来，我们可以创建 L2 缓存并将其连接到 L2 总线和内存总线。</p>
<pre><code>system.l2cache = L2Cache()
system.l2cache.connectCPUSideBus(system.l2bus)
system.l2cache.connectMemSideBus(system.membus)
</code></pre>
<p>文件中的其他所有内容都保持不变！现在我们有了一个带有两级缓存层次结构的完整配置。如果您运行当前文件，<code>hello</code>现在应该在 57467000 个滴答后完成。完整的脚本可以在<a href="https://gem5.googlesource.com/public/gem5/+/refs/heads/stable/configs/learning_gem5/part1/two_level.py">`configs/learning_gem5/part1/two_level.py</a>的 gem5 源代码中 <a href="https://gem5.googlesource.com/public/gem5/+/refs/heads/stable/configs/learning_gem5/part1/two_level.py">找到</a>。</p>
<h2 id="向脚本添加参数"><a class="header" href="#向脚本添加参数">向脚本添加参数</a></h2>
<p>使用 gem5 进行实验时，您不希望每次要使用不同参数测试系统时都编辑配置脚本。为了解决这个问题，您可以将命令行参数添加到您的 gem5 配置脚本中。同样，因为配置脚本只是 Python，所以您可以使用支持参数解析的 Python 库。尽管 pyoptparse 已被正式弃用，但 gem5 附带的许多配置脚本都使用它而不是 pyargparse，因为 gem5 的最低 Python 版本曾经是 2.5。Python 的最低版本现在是 3.6，因此在编写不需要与当前 gem5 脚本交互的新脚本时，Python 的 argparse 是更好的选择。要开始使用 :pyoptparse，您可以查阅在线 Python 文档。</p>
<p>要为我们的两级缓存配置添加选项，在导入我们的缓存后，让我们添加一些选项。</p>
<pre><code>import argparse

parser = argparse.ArgumentParser(description='A simple system with 2-level cache.')
parser.add\_argument(&quot;binary&quot;, default=&quot;&quot;, nargs=&quot;?&quot;, type=str,
                    help=&quot;Path to the binary to execute.&quot;)
parser.add\_argument(&quot;--l1i_size&quot;,
                    help=f&quot;L1 instruction cache size. Default: 16kB.&quot;)
parser.add\_argument(&quot;--l1d_size&quot;,
                    help=&quot;L1 data cache size. Default: Default: 64kB.&quot;)
parser.add\_argument(&quot;--l2_size&quot;,
                    help=&quot;L2 cache size. Default: 256kB.&quot;)

options = parser.parse\_args()
</code></pre>
<p>现在，您可以运行 <code>build/X86/gem5.opt configs/tutorial/two_level.py --help</code>它将显示您刚刚添加的选项。</p>
<p>接下来，我们需要将这些选项传递给我们在配置脚本中创建的缓存。为此，我们将简单地更改 two_level_opts.py 以将选项作为参数传递到缓存中，然后添加一个适当的构造函数。</p>
<pre><code>system.cpu.icache = L1ICache(options)
system.cpu.dcache = L1DCache(options)
...
system.l2cache = L2Cache(options)
</code></pre>
<p>在 caches.py 中，我们需要<code>__init__</code>为每个类添加构造函数（Python 中的函数）。从我们的基础 L1 缓存开始，我们将只添加一个空的构造函数，因为我们没有任何适用于基础 L1 缓存的参数。但是，在这种情况下我们不能忘记调用超类的构造函数。如果跳过对超类构造函数的调用，gem5 的 SimObject 属性查找函数将失败，<code>RuntimeError: maximum recursion depth exceeded</code>当您尝试实例化缓存对象时，结果将是“ ”。因此，<code>L1Cache</code>我们需要在静态类成员之后添加以下内容。</p>
<pre><code>def __init__(self, options=None):
    super(L1Cache, self).__init__()
    pass
</code></pre>
<p>接下来，在 中<code>L1ICache</code>，我们需要使用我们创建的选项 ( <code>l1i_size</code>) 来设置大小。在下面的代码中，如果<code>options</code>没有传递给<code>L1ICache</code>构造函数，并且在命令行上没有指定选项，则存在保护。在这些情况下，我们将只使用我们已经为大小指定的默认值。</p>
<pre><code>def __init__(self, options=None):
    super(L1ICache, self).__init__(options)
    if not options or not options.l1i_size:
        return
    self.size = options.l1i_size
</code></pre>
<p>我们可以使用相同的代码<code>L1DCache</code>：</p>
<pre><code>def __init__(self, options=None):
    super(L1DCache, self).__init__(options)
    if not options or not options.l1d_size:
        return
    self.size = options.l1d_size
</code></pre>
<p>和统一的<code>L2Cache</code>：</p>
<pre><code>def __init__(self, options=None):
    super(L2Cache, self).__init__()
    if not options or not options.l2_size:
        return
    self.size = options.l2_size
</code></pre>
<p>通过这些更改，您现在可以从命令行将缓存大小传递到您的脚本中，如下所示。</p>
<pre><code>build/X86/gem5.opt configs/tutorial/two_level.py --l2_size='1MB' --l1d_size='128kB'
gem5 Simulator System.  http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 21.0.0.0
gem5 compiled May 17 2021 18:05:59
gem5 started May 18 2021 00:00:33
gem5 executing on amarillo, pid 83118
command line: build/X86/gem5.opt configs/tutorial/two_level.py --l2_size=1MB --l1d_size=128kB

Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
0: system.remote_gdb: listening for remote gdb on port 7005
Beginning simulation!
info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 57467000 because exiting with last active thread context
</code></pre>
<p>完整的脚本可以在gem5源发现 <a href="https://gem5.googlesource.com/public/gem5/+/refs/heads/stable/configs/learning_gem5/part1/caches.py"><code>configs/learning_gem5/part1/caches.py</code></a>和 <a href="https://gem5.googlesource.com/public/gem5/+/refs/heads/stable/configs/learning_gem5/part1/two_level.py"><code>configs/learning_gem5/part1/two_level.py</code></a>。</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../part1/part1_2_simple_config.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>
                            <a rel="next" href="../part1/part1_4_gem5_stats.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>
                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../part1/part1_2_simple_config.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>
                    <a rel="next" href="../part1/part1_4_gem5_stats.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>

        <script type="text/javascript">
            window.playground_copyable = true;
        </script>
        <script src="../elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../searcher.js" type="text/javascript" charset="utf-8"></script>
        <script src="../clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="../highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="../book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->
    </body>
</html>
